<script>
import globalUtils from "../../res/www.a9a.cn";
import JpCropperWatermark
  from "../../uni_modules/jp-cropper-watermark/components/jp-cropper-watermark/jp-cropper-watermark.vue";
var n = require('../../@babel/runtime/helpers/defineProperty');
var t = require('../../res/www.a9a.cn.js');
var app = getApp();
var a = uni.getStorageSync('COOK_U');

export default {
  name: "bankAuthentication",
  computed: {
    globalUtils() {
      return globalUtils
    }
  },
  components: {JpCropperWatermark},
  data() {
    return {
      nTitle: '账户/认证',
      color: '',
      rgbcolor: '',
      D: {
        mob_rz_class: '',
        alipay_username: '',
        alipay_truename: '',
        bank_name: '',
        bank_name_kaihu: '',
        bank_card: '',
        id_card_num: '',
        bank_truename: '',
        bank_phone: '',
        mob_hide: '',
        front_id_card: '',
        front_id_card_dbname: '',
        reverse_id_card: '',
        reverse_id_card_dbname: '',
      },
      photo: '',
      photo_is: 0,
      width: 500,
      height: 400,
      max_width: 600,
      max_height: 500,
      isFlao: {
        isZfb: false,
        isYhk: true,
      },
    }
  },
  onLoad() {
    var that = this;
    var e = this;
    app.globalData.get_config('_ZEAI').then(function (a) {
      e.setData({
        color: app.globalData._ZEAI.color,
        rgbcolor: t.c16_rgb(app.globalData._ZEAI.color)
      });
    });
    app.globalData.get_openid().then(function (a) {
      that.init();
    });
  },
  methods: {
    init: function () {
      var that = this;
      a = uni.getStorageSync('COOK_U');
      app.globalData.ajax({
        url: 'tg.php',
        data: {
          submitok: 'myinfo_BANK',
          openid: a.openid
        },
        success: function (a) {
          if (1 == a.flag) {
            that.setData({
              D: a.D
            });
            that.D.reverse_id_card_dbname = that.extractPathAfterBase(a.D.reverse_id_card, app.globalData.WWW + 'up/')
            that.D.front_id_card_dbname = that.extractPathAfterBase(a.D.front_id_card, app.globalData.WWW + 'up/')
            console.log(that.D.reverse_id_card_dbname);
            console.log(that.D.front_id_card_dbname);
          } else {
            uni.showToast({
              title: a.msg,
              icon: 'none',
              mask: true
            });
          }
        }
      });
    },

    // 截取上传路径
    extractPathAfterBase(url, basePath) {
      if (!url || !basePath) return '';
      const index = url.indexOf(basePath);
      return index !== -1 ? url.substring(index + basePath.length) : '';
    },
    myinfoBANKsubmitFn: function (e) {
      var that = this;
      var s = e.detail.value;

      let arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //加权因子
      let arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2]; //校验码

      if (app.globalData.empty(s.bank_truename)) return uni.showToast({
        title: '请输入【卡号姓名】',
        icon: 'none',
        mask: true
      });
      if (app.globalData.empty(s.bank_phone)) return uni.showToast({
        title: '请输入【银行卡绑定手机号】',
        icon: 'none',
        mask: true
      });
      if (/^\d{17}\d|x$/i.test(s.id_card_num)) {
        let sum = 0,
            idx;
        for (let i = 0; i < s.id_card_num.length - 1; i++) {
          // 对前17位数字与权值乘积求和
          sum += parseInt(s.id_card_num.substr(i, 1), 10) * arrExp[i];
        }
        // 计算模（固定算法）
        idx = sum % 11;
        // 检验第18为是否与校验码相等
        if (arrValid[idx] == s.id_card_num.substr(17, 1).toUpperCase()) {

        } else {
          uni.showToast({
            title: '身份证号码错误！',
            icon: 'none',
            duration: 2000
          });
          return;
        }
      } else {
        uni.showToast({
          title: '身份证号码不正确！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      if (app.globalData.empty(s.bank_card)) return uni.showToast({
        title: '请输入【银行卡号】',
        icon: 'none',
        mask: true
      });
      if (app.globalData.empty(s.front_id_card_dbname)) return uni.showToast({
        title: '请上传【身份证(人像面)】',
        icon: 'none',
        mask: true
      });
      if (app.globalData.empty(s.reverse_id_card_dbname)) return uni.showToast({
        title: '请上传【身份证(国徽面)】',
        icon: 'none',
        mask: true
      });
      app.globalData.ajax({
        url: 'tg.php',
        data: {
          submitok: 'myinfo_BANK_modupdate',
          openid: a.openid,
          alipay_truename: s.alipay_truename,
          alipay_username: s.alipay_username,
          bank_card: s.bank_card,
          id_card_num: s.id_card_num,
          bank_name: s.bank_name,
          bank_name_kaihu: s.bank_name_kaihu,
          bank_truename: s.bank_truename,
          bank_phone: s.bank_phone,
          front_id_card: s.front_id_card_dbname,
          reverse_id_card: s.reverse_id_card_dbname,
        },
        success: function (a) {

          var o;
          if (1 == a.flag) {
            console.log('获取值')
            uni.showModal({
              content: '根据国家规定，银行卡信息需完成认证签约，请先完成签署。未签约将导致提现失败，请长按下方二维码签约',
              confirmColor: that.color,
              confirmText: '朕知道了',
              showCancel: false,
              success: function (res) {
                uni.showToast({
                  title: a.msg,
                  mask: true,
                  duration: 3000
                })
                that.setData(
                    (n((o = {}), 'D.alipay_truename', s.alipay_truename),
                        n(o, 'D.alipay_username', s.alipay_username),
                        n(o, 'D.bank_card', s.bank_card),
                        n(o, 'D.id_card_num', s.id_card_num),
                        n(o, 'D.bank_name', s.bank_name),
                        n(o, 'D.bank_name_kaihu', s.bank_name_kaihu),
                        n(o, 'D.bank_truename', s.bank_truename),
                        n(o, 'D.bank_phone', s.bank_phone),
                        n(o, 'D.front_id_card_dbname', s.front_id_card_dbname),
                        n(o, 'D.reverse_id_card_dbname', s.reverse_id_card_dbname),
                        o)
                );
                // const url = 'http://m.yunhuotec.com/cloudgainMobile/#/contractedUser?platformId=20250806984547229510574080'
                // if (res.confirm) {
                //   uni.navigateTo({
                //     url: `/pagesB/externalUrl/externalUrl?url=${url}&title=认证签约`,
                //   })
                // } else if (res.cancel) {
                //   uni.showToast({
                //     title: a.msg,
                //     mask: true,
                //     duration: 3000
                //   })
                //   that.setData(
                //       (n((o = {}), 'D.alipay_truename', s.alipay_truename),
                //           n(o, 'D.alipay_username', s.alipay_username),
                //           n(o, 'D.bank_card', s.bank_card),
                //           n(o, 'D.id_card_num', s.id_card_num),
                //           n(o, 'D.bank_name', s.bank_name),
                //           n(o, 'D.bank_name_kaihu', s.bank_name_kaihu),
                //           n(o, 'D.bank_truename', s.bank_truename),
                //           n(o, 'D.bank_phone', s.bank_phone),
                //           n(o, 'D.front_id_card_dbname', s.front_id_card_dbname),
                //           n(o, 'D.reverse_id_card_dbname', s.reverse_id_card_dbname),
                //           n(o, 'div_myinfoBANK_form', false),
                //           o)
                //   );
                // }
              }
            });

          } else {
            (a.this = that), (a.url = 'no'), t.sorry(a);
          }
        }
      });
    },
    // 上传身份证图像
    my_info_photo_s_addFn: function (n) {
      var that = this;
      uni.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        sizeType: ['original', 'compressed'],
        success: function (e) {
          that.setData({
            photo: e.tempFiles[0].tempFilePath,
            photo_is: n,
          });
        }
      });
    },
    // 裁剪后的图片
    onok(ev) {
      const that = this;
      uni.uploadFile({
        url: app.globalData.WWW + '/api/wxxcx/tg.php',
        filePath: ev.path,
        name: 'pic',
        formData: {
          submitok: 'myinfo_id_card',
          openid: a.openid
        },
        success: function (t) {
          console.log('有数据吗', t)
          that.photo = ''
          const data = JSON.parse(t.data);
          if ('1' === data.flag) {
            console.log(1)
            if (that.photo_is === 1) {
              console.log(2)
              that.setData({
                'D.front_id_card': data.id_card_photo_s,
                'D.front_id_card_dbname': data.dbname
              })
              console.log('身份证照片',that.D.front_id_card)
            } else {
              that.setData({
                'D.reverse_id_card': data.id_card_photo_s,
                'D.reverse_id_card_dbname': data.dbname
              })
            }
          } else {
            uni.showModal({
              content: data.msg,
              confirmColor: that.color,
              confirmText: '朕知道了',
              showCancel: false,
              success: function (t) {
              }
            });
          }
        }
      });
    },
    oncancel() {
      this.photo = ''
    },
    // 预览图片
    datu(img) {
      uni.previewImage({
        current: img,
        urls: [img]
      });
    },
  }
}
</script>

<template>
  <view class="bankAuthentication">
    <view class="div_myinfo_box">
      <form class="myinfo" @submit="myinfoBANKsubmitFn">
        <view v-show="isFlao.isZfb" class="zfb">
          <view class=" blue">·支付宝·</view>
          <view class="dl">
            <view class="dt">支付宝账号</view>
            <view class="dd">
              <input :value="D.alipay_username" autocomplete="off" class="input" maxlength="30" name="alipay_username"
                     placeholder="输入支付宝账号"/>
            </view>
          </view>
          <view class="dl">
            <view class="dt">支付宝姓名</view>
            <view class="dd">
              <input :value="D.alipay_truename" autocomplete="off" class="input" maxlength="50" name="alipay_truename"
                     placeholder="输入支付宝姓名"/>
            </view>
          </view>
        </view>
        <view v-show="isFlao.isYhk" class="yhk">
          <view class=" Cf00" style="color: #fc7a64">·银行卡·</view>
          <view>开户手机号为银行卡办理时绑定的手机号</view>
          <view class="dl">
            <view class="dt">卡号姓名</view>
            <view class="dd">
              <input :value="D.bank_truename" autocomplete="off" class="input" maxlength="50" name="bank_truename"
                     placeholder="输入卡号姓名"/>
            </view>
          </view>
          <view class="dl">
            <view class="dt">手机号码</view>
            <view class="dd">
              <input  :value="D.bank_phone" autocomplete="off" class="input" type="number" maxlength="11" name="bank_phone"
                     placeholder="输入银行卡绑定电话号"/>
            </view>
          </view>
          <view class="dl">
            <view class="dt">身份证号</view>
            <view class="dd">
              <input :value="D.id_card_num" autocomplete="off" class="input" maxlength="50" name="id_card_num"
                     placeholder="输入身份证号"/>
            </view>
          </view>
          <view class="dl">
            <view class="dt">银行卡号</view>
            <view class="dd">
              <input :value="D.bank_card" autocomplete="off" class="input" maxlength="50" name="bank_card"
                     placeholder="输入银行卡号"/>
            </view>
          </view>
          <view class="dl">
            <view class="dt">银行名称</view>
            <view class="dd">
              <input :value="D.bank_name" autocomplete="off" class="input" maxlength="30" name="bank_name"
                     placeholder="输入银行名称"/>
            </view>
          </view>
          <view class="dl">
            <view class="dt">开户行名称</view>
            <view class="dd">
              <input :value="D.bank_name_kaihu" autocomplete="off" class="input" maxlength="50" name="bank_name_kaihu"
                     placeholder="输入开户行名称"/>
            </view>
          </view>
          <view class="dl">
            <view class="dt ">身份证(人像面)</view>
            <view class="dd photo_sbtn" @tap="my_info_photo_s_addFn(1)">
              <text v-if="globalUtils.empty(D.front_id_card)" class="photo_tips ico">点击上传身份证</text>
              <image v-else :src="D.front_id_card" mode="heightFix"></image>
              <input v-show="false" :value="D.front_id_card_dbname" name="front_id_card_dbname"/>
            </view>
          </view>
          <view class="dl ">
            <view class="dt">身份证(国徽面)</view>
            <view class="dd photo_sbtn" @tap="my_info_photo_s_addFn(2)">
              <text v-if="globalUtils.empty(D.reverse_id_card)" class="photo_tips ico">点击上传身份证</text>
              <image v-else :src="D.reverse_id_card" mode="heightFix"></image>
              <input v-show="false" :value="D.reverse_id_card_dbname" name="reverse_id_card_dbname"/>
            </view>
          </view>
          <view class="certification" v-if="D.yunhuo_sign" >
            <view class="certificationLeft">长按保存下方二维码扫码认证签约</view>
            <view class="certificationRight">
              <image :showMenuByLongpress="true" :show-menu-by-longpress="true" :src="D.yunhuo_sign" class="certificationRightimg" mode="aspectFit"></image>
            </view>
          </view>
        </view>
        <button :style="'--color:' + color + ';--rgbcolor:' + rgbcolor + ';'" class="btn4 ico" formType="submit">
          保存
        </button>
      </form>
    </view>
    <jp-cropper-watermark :height="height" :maxHeight="max_width" :maxWidth="max_height" :url="photo" :width="width"
                          mode="free" @cancel="oncancel" @ok="onok"></jp-cropper-watermark>
  </view>
</template>

<style lang="scss" scoped>
@import "bankAuthentication.scss";
</style>